package com.pxene.dmp.test.mr.matchUer1;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

import com.pxene.dmp.common.StringUtils;
import com.pxene.dmp.constant.PatternList;
import com.pxene.dmp.constant.PatternList.Param;
import com.pxene.dmp.constant.PatternList.Pattern;

public class MatchUser1Mapper extends Mapper<LongWritable, Text, Text, Text> {
	private Log log = LogFactory.getLog(MatchUser1Mapper.class);
	private static String FILTERREGEX = ".*[\u4e00-\u9fa5]+.*"; // 过滤掉有乱码的搜索词条
	private Text resultKey = new Text();
	private Text resultVal = new Text();
	private static final String[] KEY_WORD = { "keyword", "query", "word", "kw", "wd", "q", "p", "w" };
	private static final String REGEX_TEMPLATE = "[?&]${key}=(.*?)(&|$)";
	private MultipleOutputs<Text, Text> mos;
	private static List<String> searchWords = new ArrayList<>();
	// private static String[] engines = { "baidu.com", "google.com",
	// "sogou.com", "soso.com", "yahoo.cn", "yodao.com", "bing.com" };

	private static final String AUTO_UID_REGEX1 = ".*app.autohome.com.cn.*\\&u=([\\d]*)\\&.*";
	private static final String AUTO_UID_REGEX2 = ".*app.autohome.com.cn.*\\&memberid=([\\d]*)\\&.*";
	private static final String AUTO_UID_REGEX3 = ".*app.autohome.com.cn.*\\&uid=([\\d]*)\\&.*";
	private static final String AUTO_UID_REGEX4 = ".*app.autohome.com.cn.*\\&userid=([\\d]*)\\&.*";
	private static final String AUTO_UID_REGEX5 = ".*.*app.autohome.com.cn.*-u([\\d]*)\\.json.*";
	private static String UID_MATCH_REGEX = AUTO_UID_REGEX1 + "|" + AUTO_UID_REGEX2 + "|" + AUTO_UID_REGEX3 + "|" + AUTO_UID_REGEX4 + "|" + AUTO_UID_REGEX5;
	private static String[] UID_REGEXS = { AUTO_UID_REGEX1, AUTO_UID_REGEX2, AUTO_UID_REGEX3, AUTO_UID_REGEX4, AUTO_UID_REGEX5 };

    private static List<String> bbsRegexs=new ArrayList<>();
	@Override
	protected void setup(Context context) throws IOException, InterruptedException {
		mos = new MultipleOutputs<Text, Text>(context);
		bbsRegexs = IOUtils.readLines(MatchUser1Mapper.class.getResourceAsStream("/regex/bbsRegex.txt"));
	}

	private static final String SEARCH_WORD_REGEX = ".*(\\?|&)(q=|word=|query=|kw=|wd=|keyword=).*";

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		String[] data = (value.toString() + " ").split("\\|");
		if (data[0].trim().length() == 0) {
			return;
		}
		String imsi = "";
		String mdn = "";
		String destinationURL = "";
		String domainName = "";
		log.info("#####" + data[0]);
		if (data.length == 38) {
			imsi = data[0].trim();
			mdn = data[1].trim();
			destinationURL = URLDecoder.decode(URLDecoder.decode(data[27].trim().replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "GB2312").replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8");
			domainName = data[29].trim().length() == 0 ? data[28].trim() : data[29].trim();

			// 搜索关键词提取
			if (data[27].trim().matches(SEARCH_WORD_REGEX)) {
				for (int i = 0; i < KEY_WORD.length; i++) {
					String regex = REGEX_TEMPLATE.replace("${key}", KEY_WORD[i]);
					String word = StringUtils.regexpExtract(data[27].trim(), regex).trim();
//					if (word.length() > 3 && word.length() <= 40) {
						word = URLDecoder.decode(URLDecoder.decode(word.trim().replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "GB2312").replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8");
						if (word.contains("�")) {
							word = URLDecoder.decode(URLDecoder.decode(word.trim().replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8").replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "GB2312");
						}
						if (word.contains("�")) {
							continue;
						}
						// 不含中文，过滤掉
						if (!word.matches(".*[\u4e00-\u9fa5]+.*")) {
							continue;
						}
						// log.info("############" + word);
						resultKey.set(imsi+"|"+mdn);
						resultVal.set(word);
						// context.write(resultKey, resultVal);
						mos.write(resultKey, resultVal, "searchword/");
						return;
//					}
				}
			}

		}
		if (data.length == 40) {
			imsi = data[0].trim();
			mdn = data[1].trim();
			destinationURL = URLDecoder.decode(URLDecoder.decode(data[29].trim().replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "GB2312").replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8");
			domainName = data[31].trim().length() == 0 ? data[30].trim() : data[31].trim();
			// 搜索关键词提取
			if (data[29].trim().matches(SEARCH_WORD_REGEX)) {
				for (int i = 0; i < KEY_WORD.length; i++) {
					String regex = REGEX_TEMPLATE.replace("${key}", KEY_WORD[i]);
					String word = StringUtils.regexpExtract(data[29].trim(), regex).trim();
//					if (word.length() > 3 && word.length() <= 40) {
						word = URLDecoder.decode(URLDecoder.decode(word.trim().replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "GB2312").replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8");
						if (word.contains("�")) {
							word = URLDecoder.decode(URLDecoder.decode(word.trim().replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8").replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "GB2312");
						}
						if (word.contains("�")) {
							continue;
						}
						// 不含中文，过滤掉
						if (!word.matches(".*[\u4e00-\u9fa5]+.*")) {
							continue;
						}
						// log.info("############" + word);
						resultKey.set(imsi+"|"+mdn);
						resultVal.set(word);
						// context.write(resultKey, resultVal);
						mos.write(resultKey, resultVal, "searchword/");
						return;
					}
//				}
			}

		}
		// 查看过汽车信息的
		String city = "";
		String auto = "";
		PatternList autoInfoList = PatternList.load("temp.json");
		for (Pattern autoInfo : autoInfoList.elements()) {
			if (destinationURL.matches(autoInfo.getRegex())) {
				log.info("################" + destinationURL);
				for (Pattern autoinfo : autoInfoList.elements()) {
					Param[] params = autoinfo.getParams();
					for (int i = 0; i < params.length; i++) {
						if ("city".equals(params[i].getId())) {
							city = StringUtils.regexpExtract(destinationURL, params[i].getExpr()).trim();
						}
						if ("auto".equals(params[i].getId())) {
							auto = StringUtils.regexpExtract(destinationURL, params[i].getExpr()).trim();
						}

						if (auto.length() > 0) {
							resultKey.set(imsi+"|"+mdn);
							resultVal.set(autoInfo.getCode() + "|" + auto);
							mos.write(resultKey, resultVal, "autocode/");
							return;
						}
					}
				}

			}
		}

		// 搜集汽车之家app的用户id
		if (destinationURL.matches(UID_MATCH_REGEX)) {
			for (String regex : UID_REGEXS) {
				String uid = StringUtils.regexpExtract(destinationURL, regex);
				if (uid.length() > 0) {
					if (regex.contains("autohome")) {
						resultVal.set("autohome" + "|" + uid);
					}
					resultKey.set(imsi+"|"+mdn);
					mos.write(resultKey, resultVal, "uid/");
					return;
				}
			}
		}

		//访问过汽车网站论坛
		for (String regexInfo : bbsRegexs) {
			String domain=regexInfo.split("---")[0];
			String regex=regexInfo.split("---")[1];
			String bbsId=StringUtils.regexpExtract(destinationURL, regex);
			if(bbsId.length()>0){
				resultKey.set(imsi+"|"+mdn);
				resultVal.set(domain + "|" + bbsId);
				mos.write(resultKey, resultVal, "bbsid/");
				return;
			}
		}
		// 访问过汽车之家某一论坛
//		if (destinationURL.matches(".*autohome.com.cn.*-b([\\d]*)-.*")) {
//			String bbsId = URLUtils.regexpExtract(destinationURL, ".*autohome.com.cn.*-b([\\d]*)-.*");
//			if (bbsId.length() > 0) {
//				resultKey.set(imsi+"|"+mdn);
//				resultVal.set("autohome" + "|" + bbsId);
//				mos.write(resultKey, resultVal, "bbsid/");
//				return;
//			}
//		}

	}

	@Override
	protected void cleanup(Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
		mos.close();
	}
	
	public static void main(String[] args) throws Exception {
//		PatternList autoInfoList = PatternList.load("temp.json");
//		List<String> urls=FileUtils.readLines(new File("./autourl.txt"));
//		for (String url : urls) {
//			// 查看过汽车信息的
//			String city = "";
//			String auto = "";
//			for (Pattern autoInfo : autoInfoList.elements()) {
//				if (url.matches(autoInfo.getRegex())) {
//					for (Pattern autoinfo : autoInfoList.elements()) {
//						Param[] params = autoinfo.getParams();
//						for (int i = 0; i < params.length; i++) {
//							if ("city".equals(params[i].getId())) {
//								city = URLUtils.regexpExtract(url, params[i].getExpr()).trim();
//							}
//							if ("auto".equals(params[i].getId())) {
//								auto = URLUtils.regexpExtract(url, params[i].getExpr()).trim();
//							}
//
//							if (auto.length() > 0) {
//								System.out.println(auto+"-------"+url);
//								return;
//							}
//						}
//					}
//
//				}
//			}
//		}
		
		//论坛id本地测试
//		List<String> bbsRegexs = IOUtils.readLines(MatchUser1Mapper.class.getResourceAsStream("/regex/bbsRegex.txt"));
//		List<String> bbsurls=FileUtils.readLines(new File("./luntanurl.txt"));
//		for (String url : bbsurls) {
//			for (String regexInfo : bbsRegexs) {
//				String domain=regexInfo.split("   ")[0];
//				String regex=regexInfo.split("   ")[1];
//				String bbsid=URLUtils.regexpExtract(url, regex);
//				if(bbsid.length()>0){
//					System.out.println(domain+"---"+bbsid);
//				}
//			}
//		}
	}
	
	

}
